import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login'
import Home from '../views/Home'
import Welcome from '../views/Welcome'
import Users from '../views/Users'
import Roles from '../views/Roles'
import Rights from '../views/Rights'
import Cate from '../views/Cate'
import Params from '../views/Params'
import Goods from '../views/Goods'
import Add from '../views/Add'
import Orders from '../views/Orders'
import Reports from '../views/Reports'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  { 
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/roles', component: Roles },
      { path: '/rights', component: Rights },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: Goods },
      { path: '/goods/add', component: Add },
      { path: '/orders', component: Orders },
      { path: '/reports', component: Reports }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if(to.path === '/login') {
    next()
    return 
  }

  const token = sessionStorage.getItem('token')
  
  if(token) {
    next()
  } else {
    next('/login')
  }

})

export default router
